﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Web_Oto.Bases;

namespace Web_Oto.Data
{
    public class SeoDal : dalObject
    {
        #region constructors

        public SeoDal(string connectionstring, string connectiontype)
            : base(connectionstring, connectiontype)
        {

        }

        public SeoDal(IDbConnection sqlConnection, string connectiontype, SqlTransaction dbTransaction)
            : base(sqlConnection, connectiontype, dbTransaction)
        {

        }

        #endregion

        #region methods

        public IList<SEO> GetSeos()
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.SEOs.ToList();
            }
        }

        public SEO GetSeoPage(string pageName)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                return db.SEOs.FirstOrDefault(s => s.Page.Contains(pageName));
            }
        }

        public int Update(SEO seo)
        {
            using (var db = new Web_OtoDataContext(ConfigurationManager.AppSettings["SqlConnect"]))
            {
                var result = 1;
                try
                {
                    var seoDB = db.SEOs.FirstOrDefault(p => p.Id == seo.Id);
                    if (seoDB != null)
                    {
                        seoDB.Title = seo.Title;                       
                        seoDB.Keyword = seo.Keyword;
                        seoDB.Description = seo.Description;
                        db.SubmitChanges();
                    }
                }
                catch (Exception)
                {
                    result = -1;
                }
                return result;
            }
        }
        #endregion
    }
}
